package com.sunyard.pcc.core.service.impl;

import com.sunyard.pcc.common.bo.SysRoleBO;
import com.sunyard.pcc.common.constant.ErrorCodeEnum;
import com.sunyard.pcc.common.constant.IsDeletedEnum;
import com.sunyard.pcc.common.exception.PccCommonException;
import com.sunyard.pcc.common.validator.ValidationUtils;
import com.sunyard.pcc.core.service.RoleService;
import com.sunyard.pcc.dal.mapper.ext.SysRoleExtMapper;
import com.sunyard.pcc.dal.mapper.ext.SysUserRoleExtMapper;
import com.sunyard.pcc.dal.model.SysUserRoleDO;
import com.sunyard.pcc.dal.query.SysUserRoleQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: Hewei
 * @Description:
 * @Date: Created in 16:34 2018/2/27
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private SysUserRoleExtMapper sysUserRoleExtMapper;

    @Autowired
    private SysRoleExtMapper sysRoleExtMapper;


    /**
     * 更新用户角色权限列表
     *
     * @param userId
     * @param roles
     * @return
     * @throws PccCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateUserRoles(Long userId, List<Long> roles) throws PccCommonException {

        //参数非空判断
        ValidationUtils.isBlank(userId, roles);

        //软删除原有权限关联数
        SysUserRoleQuery sysUserRoleQuery = new SysUserRoleQuery();
        sysUserRoleQuery.createCriteria()
                .andUserIdEqualTo(userId);
        SysUserRoleDO sysUserRoleDO = new SysUserRoleDO();
        sysUserRoleDO.setIsDeleted(IsDeletedEnum.UNDELETED.getStatus());
        sysUserRoleExtMapper.updateByQuerySelective(sysUserRoleDO, sysUserRoleQuery);

        //添加新权限
        checkRoles(roles);
        addUserRolesBatch(userId, roles);
        return true;
    }


    /**
     * 批量添加用户角色关联数据
     *
     * @param userId
     * @param roles
     * @return
     * @throws PccCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addUserRolesBatch(Long userId, List<Long> roles) throws PccCommonException {
        List<SysUserRoleDO> sysUserRoleDOS = new ArrayList<>();
        for (Long role : roles) {
            SysUserRoleDO sysUserRoleDO = new SysUserRoleDO();
            sysUserRoleDO.setUserId(userId);
            sysUserRoleDO.setIsDeleted(IsDeletedEnum.UNDELETED.getStatus());
            sysUserRoleDO.setRoleId(role);
            sysUserRoleDOS.add(sysUserRoleDO);
        }

        Integer count = sysUserRoleExtMapper.insertBatch(sysUserRoleDOS);

        if (sysUserRoleDOS.size() != count) {
            throw new PccCommonException(ErrorCodeEnum.INSERT_ERROR);
        }

        return true;
    }


    /**
     * 根据用户id获取用户角色列表
     *
     * @param userId
     * @return
     * @throws PccCommonException
     */
    @Override
    public List<SysRoleBO> queryUserRoles(Long userId) throws PccCommonException {
        ValidationUtils.isBlank(userId);

        List<SysRoleBO> sysRoleBOS = sysRoleExtMapper.selectUserRolesByUserId(userId);

        return sysRoleBOS;
    }


    /**
     * 验证角色数据数据是否正确
     *
     * @param roles
     * @return
     * @throws PccCommonException
     */
    private Boolean checkRoles(List<Long> roles) throws PccCommonException {
        ValidationUtils.isBlank(roles);
        Integer count = sysRoleExtMapper.selectRolesCount(roles);
        if (roles.size() != count) {
            throw new PccCommonException(ErrorCodeEnum.ROLE_DATA_ERROR);
        }
        return true;
    }


}
